//------------------------------------------------------------- // Filename: formatted2.cpp // Purpose: Demonstrate filling parallel arrays from a file // Author: Susan Gauch //------------------------------------------------------------- #include #include using namespace std; const int MAXSTUDENTS = 100; //------------------------------------------------------------- // Name: FillArrays // Purpose: Fill three arrays from a file that is already open // Also set NumStudents to the number of records read //------------------------------------------------------------- void FillArrays(ifstream & Din, int IDs[], float GPAs[], string Names[], int &NumStudents) { NumStudents = 0; while ((NumStudents < MAXSTUDENTS) && (Din >> IDs[NumStudents] >> GPAs[NumStudents] >> Names[NumStudents])) NumStudents++; } //------------------------------------------------------------- // Name: PrintStudent // Purpose: Prints the information for one student //------------------------------------------------------------- void PrintStudent(const int IDs[], const float GPAs[], const string Names[], const int StudentNum) { cout << IDs[StudentNum] << " " << GPAs[StudentNum] << " " << Names[StudentNum] << endl; } //------------------------------------------------------------- // Name: PrintArrays // Purpose: Print three arrays to the screen //------------------------------------------------------------- void PrintArrays(const int IDs[], const float GPAs[], const string Names[], const int NumStudents) { for (int s = 0; s < NumStudents; s++) PrintStudent(IDs, GPAs, Names, s); } //------------------------------------------------------------- // Name: CalcAverage // Purpose: Return the average GPA for the students //------------------------------------------------------------- float CalcAverage(const float GPAs[], const int NumStudents) { // Add all the GPAs together float Sum = 0; for (int s = 0; s < NumStudents; s++) Sum += GPAs[s]; return Sum / NumStudents; } //------------------------------------------------------------- // Name: FindTopStudent // Purpose: Return the location of the top student //------------------------------------------------------------- int FindTopStudent(const float GPAs[], const int NumStudents) { // Search array for highest GPA int TopLoc = 0; for (int s = 1; s < NumStudents; s++) if (GPAs[s] > GPAs[TopLoc]) TopLoc = s; return TopLoc; } //------------------------------------------------------------- // Name: main // Purpose: Read student data and print information //------------------------------------------------------------- int main() { ifstream Din; string Filename; int IDs[MAXSTUDENTS]; float GPAs[MAXSTUDENTS]; string Names[MAXSTUDENTS]; int NumStudents = 0; float Average; int TopLoc; // Get the filename from the user cout << "Enter the data file's name: "; cin >> Filename; // Open the student data file Din.open(Filename.c_str()); if (Din.fail()) cerr << "Error, " << Filename << " did not exist.\n"; else { // Read all student data FillArrays(Din, IDs, GPAs, Names, NumStudents); Din.close(); // Print all student data cout << "\nThe file contained: \n"; PrintArrays(IDs, GPAs, Names, NumStudents); // Calculate the class average Average = CalcAverage(GPAs, NumStudents); cout << "\nThe class average is: " << Average << endl; // Find the top student TopLoc = FindTopStudent(GPAs, NumStudents); cout << "The top student is: "; PrintStudent(IDs, GPAs, Names, TopLoc); } return 0; }